package su.user.service.impl;

import java.util.List;
import java.util.Map;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import su.base.Constants;
import su.tool.AreaUtil;
import su.tool.SuIntUtils;
import su.user.dao.IUserCustomerDAO;
import su.user.dao.IUserFhzDAO;
import su.user.dao.IUserGhsDAO;
import su.user.dao.IUserYwyDAO;
import su.user.entity.UserCustomer;
import su.user.entity.UserFhz;
import su.user.entity.UserGhs;
import su.user.entity.UserYwy;
import su.user.service.UserService;
import su.user.util.YwyCache;

public class UserServiceImpl implements UserService {
	
	@Autowired
	private IUserYwyDAO ywyDAO;

	@Autowired
	private IUserGhsDAO ghsDAO;

	@Autowired
	private IUserFhzDAO fhzDAO;

	@Autowired
	private IUserCustomerDAO customerDAO;

	@Autowired
	private YwyCache ywyCache; 
	
	protected final Log log = LogFactory.getLog(getClass());
	//============================
	public List<UserYwy> searchYwy(PagerDTO s){
		List<UserYwy> list = null;
		try {
			list = ywyDAO.queryByPage(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	public UserYwy getYwyById(int id){
		UserYwy ywy = null;
		try {
			ywy = ywyDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return ywy;
	}

	public void saveYwy(UserYwy o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			ywyDAO.insert(o);
		}else{
			ywyDAO.update(o);
		}
	}
	public void removeYwy(int id)throws Exception{
		ywyDAO.remove(id);
	}
	
	//============================================================
	
	public List<UserGhs> searchGhsByPage(PagerDTO s){
		List<UserGhs> list = null;
		try {
			list = ghsDAO.queryByPage(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	public UserGhs getGhsById(int id){
		UserGhs ghs = null;
		try {
			ghs = ghsDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return ghs;
	}

	public void saveGhs(UserGhs o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			ghsDAO.insert(o);
		}else{
			ghsDAO.update(o);
		}
	}
	
	public void removeGhs(int id)throws Exception{
		ghsDAO.remove(id);
	}
	
	//======================================================
	public List<UserFhz> searchFhz(PagerDTO s){
		List<UserFhz> list = null;
		try {
			list = fhzDAO.queryByPage(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	public UserFhz getFhzById(int id){
		UserFhz fhz = null;
		try {
			fhz = fhzDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return fhz;
	}
	
	public void saveFhz(UserFhz o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			fhzDAO.insert(o);
		}else{
			fhzDAO.update(o);
		}
	}
	
	public void removeFhz(int id)throws Exception{
		fhzDAO.remove(id);
	}

	//======================================================
	
	public List<Map> searchCustomer(PagerDTO s){
		List<Map> list = null;
		try {
			list = customerDAO.queryByPage(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if(list==null || list.isEmpty())return list;
		
		for(Map one : list){
			one.put("ywyname", ywyCache.getYwynameById(SuIntUtils.getInt(one.get("ywyid"))));//设置业务员姓名
			one.put("areaname", AreaUtil.getAreaStr(SuIntUtils.getInt(one.get("provientid"))));//设置地区名称
		}
		
		return list;
	}
	public UserCustomer getCustomerById(int id){
		UserCustomer baseinfo = null;
		try {
			baseinfo = customerDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return baseinfo; 
	}
	
	
	public void saveCustomer(UserCustomer o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			customerDAO.insert(o);
		}else{
			customerDAO.update(o);
		}
	}
	
	public void removeCustomer(int id)throws Exception{
		customerDAO.remove(id);
	}
}
